﻿using Microsoft.AspNetCore.Mvc;
using System;
using yunbo.Model.BI;
using yunbo.Repositorys.BIRepositorys;

namespace yunbo.Controllers.BIControllers
{
	[Route("api/[controller]")]
	[ApiController]
	public class GCController : Controller
	{
		GCRepository gcRepository = new GCRepository();

		#region 工厂部分

		/// <summary>
		/// 获取工厂
		/// </summary>
		/// <param name="str"></param>
		/// <param name="date1"></param>
		/// <param name="date2"></param>
		/// <returns></returns>
		[HttpGet("GetGC")]
		public IActionResult GetGC(string? str, DateTime? date1, DateTime? date2)
		{
			List<Model.BI.GC> gc = gcRepository.GetGC(str, date1, date2);
			return Ok(gc);
		}


		/// <summary>
		/// 新增工厂
		/// </summary>
		/// <param name="gc"></param>
		/// <returns></returns>
		[HttpPost("AddGC")]
		public IActionResult AddGC(Model.BI.GC gc)
		{
			try
			{
				if (string.IsNullOrEmpty(gc.gc_code)
			|| string.IsNullOrEmpty(gc.gc_name)
			|| string.IsNullOrEmpty(gc.gc_address))
				{
					return Ok(new { success = false, message = "数据无效" }); // 返回明确的错误信息
				}

				bool res = gcRepository.AddGC(gc);
				return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
			}
			catch (Exception ex)
			{
				return StatusCode(500, new { success = false, message = ex.Message });
			}
		}


		/// <summary>
		/// 修改工厂
		/// </summary>
		/// <param name="gc"></param>
		/// <returns></returns>
		[HttpPut("UpdateGC")]
		public IActionResult UpdateGC(Model.BI.GC gc)
		{
			try
			{
				if (string.IsNullOrEmpty(gc.gc_code)
			|| string.IsNullOrEmpty(gc.gc_name)
			|| string.IsNullOrEmpty(gc.gc_address)
				|| gc.gc_id <= 0)
				{
					return Ok(new { success = false, message = "数据无效" }); // 返回明确的错误信息
				}

				bool res = gcRepository.UpdateGC(gc);
				return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
			}
			catch (Exception ex)
			{
				return StatusCode(500, new { success = false, message = ex.Message });
			}
		}


		/// <summary>
		/// 删除工厂
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPut("DelGC")]
		public IActionResult DelGC(int id)
		{
			bool res = gcRepository.DelGC(id);
			return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
		}


		/// <summary>
		/// 批量删除工厂
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPut("DelGCs")]
		public IActionResult DelGCs(int[] ids)
		{
			bool res = gcRepository.DelGCs(ids);
			return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
		}

		#endregion

		#region 车间部分

		/// <summary>
		/// 获取车间
		/// </summary>
		/// <param name="gc_id"></param>
		/// <param name="str"></param>
		/// <param name="date1"></param>
		/// <param name="date2"></param>
		/// <returns></returns>
		[HttpGet("GetCJ")]
		public IActionResult GetCJ(int gc_id, string? str, DateTime? date1, DateTime? date2)
		{
			List<CJ> cj = gcRepository.GetCJ(gc_id, str, date1, date2);
			return Ok(cj);
		}


		[HttpGet("GetCJbyCode")]
		public IActionResult GetCJbyCode(string? code)
		{
			List<CJ> cj = gcRepository.GetCJbyCode(code);
			return Ok(cj);
		}


		/// <summary>
		/// 新增车间
		/// </summary>
		/// <param name="cj"></param>
		/// <returns></returns>
		[HttpPost("AddCJ")]
		public IActionResult AddCJ(CJ cj)
		{
			try
			{
				// 数据验证（可选）
				if (string.IsNullOrEmpty(cj.cj_code)
					|| string.IsNullOrEmpty(cj.cj_name)
					|| cj.gc_id <= 0
					)
				{
					return Ok(new { success = false, message = "数据无效" }); // 返回明确的错误信息
				}

				bool res = gcRepository.AddCJ(cj);
				return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
			}
			catch (Exception ex)
			{
				return StatusCode(500, new
				{
					success = false,
					message = ex.Message
				});
			}
		}


		/// <summary>
		/// 修改车间
		/// </summary>
		/// <param name="cj"></param>
		/// <returns></returns>
		[HttpPut("UpdateCJ")]
		public IActionResult UpdateCJ(CJ cj)
		{
			try
			{
				if (string.IsNullOrEmpty(cj.cj_code)
				|| string.IsNullOrEmpty(cj.cj_name)
				|| cj.gc_id <= 0
				|| cj.cj_id <= 0)
				{
					return Ok(new { success = false, message = "数据无效" }); // 返回明确的错误信息
				}

				bool res = gcRepository.UpdateCJ(cj);
				return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
			}
			catch (Exception ex)
			{
				return StatusCode(500, new { success = false, message = ex.Message });
			}
		}


		/// <summary>
		/// 删除车间
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPut("DelCJ")]
		public IActionResult DelCJ(int id)
		{
			bool res = gcRepository.DelCJ(id);
			return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
		}


		/// <summary>
		/// 批量删除车间
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPut("DelCJs")]
		public IActionResult DelCJs(int[] ids)
		{
			bool res = gcRepository.DelCJs(ids);
			return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
		}

		#endregion

		#region 生产线部分

		/// <summary>
		/// 获取生产线
		/// </summary>
		/// <param name="cj_id"></param>
		/// <param name="str"></param>
		/// <param name="date1"></param>
		/// <param name="date2"></param>
		/// <returns></returns>
		[HttpGet("GetSCX")]
		public IActionResult GetSCX(int cj_id, string? str, DateTime? date1, DateTime? date2)
		{
			List<SCX> scx = gcRepository.GetSCX(cj_id, str, date1, date2);
			return Ok(scx);
		}


		/// <summary>
		/// 新增生产线
		/// </summary>
		/// <param name="scx"></param>
		/// <returns></returns>
		[HttpPost("AddSCX")]
		public IActionResult AddSCX(SCX scx)
		{
			try
			{
				if (string.IsNullOrEmpty(scx.scx_code)
				|| string.IsNullOrEmpty(scx.scx_name)
				|| scx.cj_id <= 0)
				{
					return Ok(new { success = false, message = "数据无效" }); // 返回明确的错误信息
				}

				bool res = gcRepository.AddSCX(scx);
				return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
			}
			catch (Exception ex)
			{
				return StatusCode(500, new { success = false, message = ex.Message });
			}
		}


		/// <summary>
		/// 修改生产线
		/// </summary>
		/// <param name="scx"></param>
		/// <returns></returns>
		[HttpPut("UpdateSCX")]
		public IActionResult UpdateSCX(SCX scx)
		{
			try
			{
				if (string.IsNullOrEmpty(scx.scx_code)
				|| string.IsNullOrEmpty(scx.scx_name)
				|| scx.cj_id <= 0
				|| scx.scx_id <= 0)
				{
					return Ok(new { success = false, message = "数据无效" }); // 返回明确的错误信息
				}

				bool res = gcRepository.UpdateSCX(scx);
				return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
			}
			catch (Exception ex)
			{
				return StatusCode(500, new { success = false, message = ex.Message });
			}
		}


		/// <summary>
		/// 删除生产线
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPut("DelSCX")]
		public IActionResult DelSCX(int id)
		{
			bool res = gcRepository.DelSCX(id);
			return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
		}


		/// <summary>
		/// 批量删除生产线
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPut("DelSCXs")]
		public IActionResult DelSCXs(int[] ids)
		{
			bool res = gcRepository.DelSCXs(ids);
			return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
		}

		#endregion

		#region 工位部分

		/// <summary>
		/// 获取工位
		/// </summary>
		/// <param name="scx_id"></param>
		/// <param name="str"></param>
		/// <param name="date1"></param>
		/// <param name="date2"></param>
		/// <returns></returns>
		[HttpGet("GetGW")]
		public IActionResult GetGW(int scx_id, string? str, DateTime? date1, DateTime? date2)
		{
			List<GW> gw = gcRepository.GetGW(scx_id, str, date1, date2);
			return Ok(gw);
		}


		/// <summary>
		/// 新增工位
		/// </summary>
		/// <param name="gw"></param>
		/// <returns></returns>
		[HttpPost("AddGW")]
		public IActionResult AddGW(GW gw)
		{
			try
			{
				if (string.IsNullOrEmpty(gw.gw_code)
				|| string.IsNullOrEmpty(gw.gw_name)
				|| gw.scx_id <= 0)
				{
					return Ok(new { success = false, message = "数据无效" }); // 返回明确的错误信息
				}

				bool res = gcRepository.AddGW(gw);
				return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
			}
			catch (Exception ex)
			{
				return StatusCode(500, new { success = false, message = ex.Message });
			}
		}


		/// <summary>
		/// 修改工位
		/// </summary>
		/// <param name="gw"></param>
		/// <returns></returns>
		[HttpPut("UpdateGW")]
		public IActionResult UpdateGW(GW gw)
		{
			try
			{
				if (string.IsNullOrEmpty(gw.gw_code)
				|| string.IsNullOrEmpty(gw.gw_name)
				|| gw.scx_id <= 0
				|| gw.gw_id <= 0)
				{
					return Ok(new { success = false, message = "数据无效" }); // 返回明确的错误信息
				}

				bool res = gcRepository.UpdateGW(gw);
				return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
			}
			catch (Exception ex)
			{
				return StatusCode(500, new { success = false, message = ex.Message });
			}
		}


		/// <summary>
		/// 删除工位
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[HttpPut("DelGW")]
		public IActionResult DelGW(int id)
		{
			bool res = gcRepository.DelGW(id);
			return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
		}


		/// <summary>
		/// 批量删除工位
		/// </summary>
		/// <param name="ids"></param>
		/// <returns></returns>
		[HttpPut("DelGWs")]
		public IActionResult DelGWs(int[] ids)
		{
			bool res = gcRepository.DelGWs(ids);
			return Ok(new { success = res, message = res ? "操作成功" : "操作失败" });
		}

		#endregion


	}
}
